/**
 * AtolCD/Spatialytics/widgets/HierarchyChooser.js
 *
 * 
 *
 * Copyright (C) 2010 AtolCD
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Vincent Plantegenet
 * @since 201003
 **/


dojo.provide("AtolCD.Spatialytics.widgets.HierarchyChooser");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("dijit.TitlePane");
dojo.require("dijit.form.SimpleTextarea");

dojo.declare("AtolCD.Spatialytics.widgets.HierarchyChooser",
		[dijit._Widget, dijit._Templated], {

	title: "Hierarchie",
	content: "",
	
	templatePath: dojo.moduleUrl("AtolCD.Spatialytics.widgets","templates/HierarchyChooser.html"),
	widgetsInTemplate: true,
	
	// --- private attributes:
	_solapContext: null,
	_observerFunc: null,
	_olapAjax: null,
	_config2: null,
	_currCellSet: null,
	_hierarchy : [],
	
	// -- postCreate function
	postCreate: function () {
		var that = this;
		var itemsJson = {
			identifier: 'id',
			label: 'label',
			items: []};
			
		var title= "Hierarchie";
		var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
		this.hieraWidget.attr('title', title);
		this.hierWidget.attr('searchAttr', 'label');
		this.hierWidget.attr('store', dataStore);
		this.hierWidget.attr('onChange',
				function() {
					that._handleChange();
				}
		);

		this.inherited(arguments);
	},
	
	initialize: function(config) {
		var itemsJson = {
				identifier: 'id',
				label: 'label',
				items: []};
		var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
		this.hierWidget.attr('store', dataStore);
		var hiers = config.config2.Cubes[this._solapContext.curr_cube].Dimensions[parseInt(config.config2.geom)];
		for(var y =0;y<hiers.Hierarchies.length;y++)
		{
			this.addMeasure(y,hiers.Hierarchies[y].name);
		}
	},
	// --- private methods
	_handleChange: function () {
		this._updateCurrentMap();
	},
	
	_updateCurrentMap: function () {
		var item = this._getCurrentItemAttr();

		if (item != null) {
			
			this._solapContext.current_Hierarchy = this.hierWidget.attr('item').label;
			this._solapContext.reqDone = false;
			this._currentId = this.hierWidget.attr('item').id;
			cube = this._solapContext.curr_cube;
			var mdx_req = "SELECT NON EMPTY AddCalculatedMembers({[Measures].Members}) ON COLUMNS, NON EMPTY  {["+this._solapContext.config2.Cubes[cube].Dimensions[parseInt(this._solapContext.config2.geom)].name+".";
			mdx_req += this._solapContext.config2.Cubes[cube].Dimensions[parseInt(this._solapContext.config2.geom)].Hierarchies[parseInt(this._currentId)].name+"].[";
			mdx_req += this._solapContext.config2.Cubes[cube].Dimensions[parseInt(this._solapContext.config2.geom)].Hierarchies[parseInt(this._currentId)].allmn+"].Children} ON ROWS FROM ";
			mdx_req += "["+this._solapContext.config2.Cubes[cube].name+"]";
			
			this._olapAjax.setMdxQuery(mdx_req);
			this._olapAjax.submitQuery();
			
		}
	},
	


	// --- getters/setters
	
	_setTitleAttr: function (/*String*/ title) {
		this.title = title;
		this.hieraWidget.attr('title', title);
	},

	_getCurrentItemAttr: function () {
		return this.hierWidget.attr('item');
	},
	// --- public methods
	
	setCurrentItemAttr: function (nb) {
		this.hierWidget.attr('value', nb);
	},
	
	addMeasure: function (/*integer*/ id,
	/*String*/ label) {
		
		//Mise a jour des valeurs des mesures
		this.hierWidget.attr('store').newItem({id: id, label: label});
		this._hierarchy[id] = label;
		
		if(id == 0) {
			// first MapStyle added, set the current item in the FilteringSelect
			// with the added MapStyle
			this.hierWidget.attr('value', 0);
			this._solapContext.default_Hierarchy = this.hierWidget.attr('item').label;
		}

	},
	
	attachSOLAPContext: function (/*GeoSOA.Spatialytics.SOLAPContext*/ solapContext) {
		this._solapContext = solapContext;
	},
	
	detachSOLAPContext: function () {
		if (this._solapContext != null) {
			this._solapContext = null;
		}
	},

	attachOlapAjax: function (/*GeoSOA.olap4js.OlapAjax*/ olapAjax) {
		this._olapAjax = olapAjax;
	}
});
